/*
 * @Author: tongbin.xtb
 * @Date: 2018-11-29 11:21:01
 * @Last Modified by: tongbin.xtb
 * @Last Modified time: 2018-12-03 22:03:26
 */

import { getHoverStatus } from './utils';

export function mouseEnterEdge({ graph, bpmn, ev, backUpCursor }) {
  const item = ev.item;
  const controlPoints = item.model.controlPoints;
  // 检查是在竖线还是横线上
  const { index, vertical } = getHoverStatus({
    x: ev.x,
    y: ev.y
  }, controlPoints);
  if (index != null) {
    if (backUpCursor) {
      item._cursor = bpmn.getGraph().getMouseEventWrapper().style.cursor;
    }
    graph.update(item, {
      hold: {
        index,
        vertical
      }
    });
    if (vertical) {
      bpmn.css({
        cursor: 'col-resize'
      });
    } else {
      bpmn.css({
        cursor: 'row-resize'
      });
    }
  }
}

export function mouseLeaveEdge({ graph, bpmn, item }) {
  if (item._cursor) {
    bpmn.css({
      cursor: item._cursor
    });
  }
  delete item._cursor;
  graph.update(item, {
    hold: undefined
  });
}

export function startMove(graph, ev) {
  const item = ev.item;
  graph.update(item, {
    lastMouse: {
      x: ev.x,
      y: ev.y
    }
  });
}

export function endMove({ graph, item }) {
  graph.update(item, {
    lastMouse: undefined,
    moveDelta: undefined
  });
}

export function mouseMoveEdge(graph, item, ev) {
  let delta;
  if (item.model && item.model.hold && item.model.hold.vertical) {
    delta = {
      x: ev.x - item.model.lastMouse.x,
      y: 0
    };
  } else {
    delta = {
      x: 0,
      y: ev.y - item.model.lastMouse.y
    };
  }
  graph.update(item, {
    edgeMoved: delta,
    modifiedByMouse: true
  });
}

/**
 *
 * @param {object} item 线对应的模型
 * @param {number} index 下标
 * @param {bool} vertical 是否垂直方向
 * @return {object} 可能的辅助线
 */
export function mergeLine(item, index, vertical) {
  const model = item.model;
  // hold index, 需要对齐的下标
  const hi = index || model.hold.index;
  const cp = model.controlPoints; // control points
  if (vertical === null) {
    vertical = model.hold.vertical; // 是否垂直
  }
  let helpLine; // 辅助线
  const needMerge = [];
  // 分四种情况讨论
  if (hi >= 2) {
    if (vertical) {
      if (Math.abs(cp[hi - 2].x - cp[hi].x) <= 3) {
        helpLine = [{
          x: cp[hi - 2].x,
          y: cp[hi - 2].y
        }, {
          x: cp[hi - 2].x,
          y: cp[hi + 1].y
        }];
        needMerge.push(hi - 1, hi);
        model.hold.index = hi - 2;
        model.controlPoints[hi + 1].x = cp[hi - 2].x;
      }
    } else {
      if (Math.abs(cp[hi - 2].y - cp[hi].y) <= 3) {
        helpLine = [{
          x: cp[hi - 2].x,
          y: cp[hi - 2].y
        }, {
          x: cp[hi + 1].x,
          y: cp[hi - 2].y
        }];
        needMerge.push(hi - 1, hi);
        model.hold.index = hi - 2;
        model.controlPoints[hi + 1].y = cp[hi - 2].y;
      }
    }
  }
  if (hi <= cp.length - 4) {
    if (vertical) {
      if (Math.abs(cp[hi].x - cp[hi + 2].x) <= 3) {
        if (!helpLine) {
          helpLine = [{
            x: cp[hi + 3].x,
            y: cp[hi].y
          }, {
            x: cp[hi + 3].x,
            y: cp[hi + 3].y
          }];
          model.controlPoints[hi].x = cp[hi + 3].x;
        } else {
          helpLine[1] = {
            x: cp[hi - 2].x,
            y: cp[hi + 3].y
          };
          model.controlPoints[hi + 3].x = cp[hi - 2].x;
        }
        needMerge.push(hi + 1, hi + 2);
      }
    } else {
      if (Math.abs(cp[hi].y - cp[hi + 2].y) <= 3) {
        if (!helpLine) {
          helpLine = [{
            x: cp[hi].x,
            y: cp[hi + 3].y
          }, {
            x: cp[hi + 3].x,
            y: cp[hi + 3].y
          }];
          model.controlPoints[hi].y = cp[hi + 3].y;
        } else {
          helpLine[1] = {
            x: cp[hi + 3].x,
            y: cp[hi - 2].y
          };
          model.controlPoints[hi + 3].y = cp[hi - 2].y;
        }
        needMerge.push(hi + 1, hi + 2);
      }
    }
  }
  if (needMerge && needMerge.length) {
    cp.splice(needMerge[0], needMerge.length);
  }
  return helpLine;
}
